package com.infomanage.service.tech;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.PositionPlanDAO;
import com.infomanage.dao.TechRescueLogDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.TechRescuelog;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.TechRescuelogItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

/**
 * 矿山救护日志
 * 
 */
public class TechRescueLogService {

	public static TechRescuelogItem loadRescueLog(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			TechRescueLogDAO dao = new TechRescueLogDAO();
			if (StringUtils.isEmpty(itemId))
				return null;
			return dao.find(Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<TechRescuelogItem> loadRescueLogList(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			TechRescueLogDAO dao = new TechRescueLogDAO();
			List<TechRescuelogItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ReturnObject saveRescueLog(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			TechRescuelog bean = new TechRescuelog();
			bean.setArrivenum(Integer.parseInt(request.getParameter("arrivenum")));
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setDesciption(request.getParameter("description"));
			bean.setDetail(request.getParameter("detail"));
			bean.setTeamleaderId(Integer.parseInt(request.getParameter("teamLeaderId")));
			bean.setTimestamp(request.getParameter("timestamp"));
			bean.setWillarrivenum(Integer.parseInt(request.getParameter("willArrivenum")));
			TechRescueLogDAO dao = new TechRescueLogDAO();
			dao.add(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject removeRescueLog(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String studyId = request.getParameter("itemId");
			TechRescueLogDAO dao = new TechRescueLogDAO();
			dao.remove(TechRescuelog.class, Integer.parseInt(studyId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject removeBatchRescueLog(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			TechRescueLogDAO dao = new TechRescueLogDAO();
			if (batchIds != null) {
				String[] ids = batchIds.split(";");
				for (String id : ids) {
					dao.remove(TechRescuelog.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject modifyRescueLog(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("itemId"));
			TechRescueLogDAO dao = new TechRescueLogDAO();
			TechRescuelog bean = dao.find(TechRescuelog.class, planId);
			bean.setArrivenum(Integer.parseInt(request.getParameter("arrivenum")));
			bean.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			bean.setDesciption(request.getParameter("description"));
			bean.setDetail(request.getParameter("detail"));
			bean.setTeamleaderId(Integer.parseInt(request.getParameter("teamLeaderId")));
			bean.setTimestamp(request.getParameter("timestamp"));
			bean.setWillarrivenum(Integer.parseInt(request.getParameter("willArrivenum")));
			dao.modify(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}

	public static List<TechRescuelogItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			TechRescueLogDAO dao = new TechRescueLogDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<TechRescuelogItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			TechRescueLogDAO dao = new TechRescueLogDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1) / PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}

	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String teamLeaderId = request.getParameter("teamLeaderId");
		String deptId = request.getParameter("deptId");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(teamLeaderId)) {
			FieldBean field = new FieldBean("teamleaderId", teamLeaderId);
			searchBean.add(field);
		}
		if (!StringUtil.isEmpty(deptId)) {
			FieldBean field = new FieldBean("deptId", deptId);
			searchBean.add(field);
		}

		// 设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
